<!--#include file="./header.inc"-->
<%
if not isSuper then
    admin.message "权限不足"
end if

dim backupDir
backupDir = getBackupDir()

select case aq
	case "backup"      	: 	call backup
	case "restore"  	: 	call restore
	case "compact"  	: 	call compact
	case "delete" 		: 	call delete
	case "download" 	: 	call download
	case else 			: 	call index
end select

call db.close()

'列表
function index()
    dim list
    list = admin.fileList(backupDir)
    infoTotal = list(0)
    temp = list(1)
    %><!--#include file="./view/database.index.html"--><%
end function

'备份
function backup()
    on error resume next
    randomize
    dim name, t
    name = root & dataPath
    t = now()
	name = year(t) & right("0" & month(t), 2) & right("0" & day(t), 2) & "_"
    name = name & right("0" & hour(t), 2) & right("0" & minute(t), 2) & right("0" & second(t), 2)
    name = name & ".mdb.back"
    fso.CopyFile server.mapPath(root & dataPath), server.mapPath(backupDir & name), true
    app.errorLog
    if err.number <> 0 then
        admin.message "备份文件失败"
    else
        admin.back "备份文件成功！<br />文件名: " & name & " ", "返回备份首页", "?q=index"
    end if
end function

'还原
function restore()
    on error resume next
	dim fn
    fn = trim(request("fn"))
    if fn = "" then
        app.json "没有文件", 1
    end if
    fn = backupDir & fn
    if app.isFile(fn) = false then
        app.json "文件不存在", 1
    end if
    fso.CopyFile server.mapPath(fn), server.mapPath(root & dataPath), true
    if err.number <> 0 then
        app.json "还原失败", 1
    else
        app.json "还原成功！", 0
    end if
end function

'删除
function delete()
    dim fns, fn, j, result
    fns = request("fn").count
    if fns = 0 then app.json "没有文件！", 1
    j = 0
    for i = 1 to fns
        fn = trim(request("fn")(i))
        result = app.deleteFile(backupDir & fn)
        if result then j = j + 1
    next
    if j > 0 then
        app.json "删除 " & j & " 个文件成功", 0
    else
        app.json "删除失败", 1
    end if
end function

'下载
function download()
    dim fn
    fn = trim(request("fn"))
    admin.download backupDir & fn
end function

'压缩
function compact()
	on error resume next
	if not app.checkObject("JRO.JetEngine") then
        admin.message "没有安装 JRO.JetEngine 组件，无法压缩数据库"
    end if
    dim now_fn, copy_fn, temp_fn, engine, prov

    now_fn = server.mapPath(root & dataPath) '现数据库文件名
    copy_fn = server.mapPath(now_fn & ".copy.tmp") '拷贝的文件名
    fso.CopyFile now_fn, copy_fn, true

    temp_fn = server.mapPath(now_fn & ".tmp") '临时文件名
    app.deleteFile temp_fn

    set engine = server.createObject("JRO.JetEngine")
    prov = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
    engine.compactDatabase prov & copy_fn, prov & temp_fn
    set engine = nothing
    fso.CopyFile temp_fn, now_fn, true
    app.deleteFile copy_fn
    app.deleteFile temp_fn
    app.errorLog
    if app.isFile(temp_fn) then
        admin.message "压缩数据库失败"
    else
        admin.message "压缩数据库成功"
    end if
end function

'取得备份文件目录
function getBackupDir()
    dim dir
    dir = root & dataPath
    dir = left(dir, inStrRev(dir, "/"))
    if dir = "" then dir = root & "core/database/"
    dir = dir & "backup/"
    call app.mkdir(dir)
    getBackupDir = dir
end function

%>